package com.rivercloud.dao.Impl;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.rivercloud.dao.UserTableDao;
import com.rivercloud.entity.UserTable;

public class UserTableDaoImpl implements UserTableDao {
	static SqlSessionFactory sqlSessionFactory = null;
	 static{
		 SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
		 InputStream inputStream =null;
		 
		 try {
			inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
	 }
	@Override
	public void addUserTable(UserTable userTable) {
		SqlSession session = sqlSessionFactory.openSession();
		session.insert("addUserTable",  userTable);
		session.commit();
		session.close();
		
	}
	@Override
	public List<UserTable> findUserTableByUserId(int userId) {
		SqlSession session = sqlSessionFactory.openSession();
		List<UserTable> selectList = session.selectList("findUserTableByUserId",  userId);
		session.close();
		return selectList;
	}
	 
	
	@Override
	public UserTable checkCollection(UserTable userTable) {
		SqlSession session = sqlSessionFactory.openSession();
		UserTable existUserTable = session.selectOne("checkCollection",  userTable);
		session.close();
		return existUserTable;
	}
	@Override
	public void deleteUserTable(UserTable userTable) {
		SqlSession session = sqlSessionFactory.openSession();
		session.delete("deleteCollection", userTable);
		session.commit();
		session.close();
		
	}

}
